//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Executor
{
    using System;
    using System.IO;
    using System.Threading;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Research.eResearch.Execution;

    /// <summary>
    /// This is used to execute the Workflows.
    /// </summary>
    public class TridentWorkflowHost
    {
        /// <summary>
        /// Workflow job id.
        /// </summary>
        private Guid jobId;

        /// <summary>
        /// Initializes a new instance of the TridentWorkflowHost class.
        /// </summary>
        /// <param name="jobId">Workflow job id.</param>
        public TridentWorkflowHost(Guid jobId)
        {
            this.jobId = jobId;
        }

        /// <summary>
        /// Main entry point for execution.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        public static int Main(string[] args)
        {
            int jobResult = -1;
            if (args.Length != 2)
            {
                Console.WriteLine("Usage:{0} -j jobId", AppDomain.CurrentDomain.FriendlyName);
                Environment.Exit(1);
            }
            try
            {
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledException);
                Guid jobId = new Guid(args[1]);
                Logger.Write("Starting TridentWorkflowHost with jobId: " + jobId, "FileLogger");

                TridentWorkflowHost app = new TridentWorkflowHost(jobId);
                jobResult = app.Execute();
            }
            catch (FormatException)
            {
                Logger.Write("Invalid job id", "FileLogger");
            }
            return jobResult;
        }

        /// <summary>
        /// Method logs the unhandled exception in the app domain.
        /// </summary>
        /// <param name="sender">Object who raises the unhandled exception.</param>
        /// <param name="e">Event argument containing exception details.</param>
        private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Logger.Write(e.ExceptionObject, "FileLogger");
        }

        /// <summary>
        /// Executes the workflow job.
        /// </summary>
        private int Execute()
        {
            int jobResult = -1;
            try
            {
                JobExecutorProxy executor = new JobExecutorProxy(jobId);
                jobResult = executor.Execute(false);
                Logger.Write("TridentWorkflowHost completed", "FileLogger");
            }
            catch (Exception exp)
            {
                Logger.Write("Exception occured while executing the job", "FileLogger");
                Logger.Write(exp, "FileLogger");
            }
            return jobResult;
        }
    }
}
